﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using dict = System.Collections.DictionaryEntry;

using nr.GLV.Windows.Controls;

namespace nr.GLV.Windows
{
    public partial class CustomersDatagrid : ArchiveUserControl
    {
        public Dictionary<string, string> PlaceHolders { get; set; }

        public CustomersDatagrid()
        {
            InitializeComponent();

            FilterFields.Clear();

            FilterFields.AddRange(
                new FilterField[]{
                    new FilterField("Cognome","LastName"),
                    new FilterField("Nome Completo", "FullName"),
                    new FilterField("Nome","FirstName"),
                    new FilterField("Città", "CityName"),
                    new FilterField("Totale da Pagare", "PaymentAmount", FieldType.Numeric),
                    new FilterField("Numero di Lampade", "Lamps", FieldType.Numeric)
                });

            PlaceHolders = new Dictionary<string, string>();
            PlaceHolders.Add("[cognome]", "LastName");
            PlaceHolders.Add("[nome]", "FirstName");
            PlaceHolders.Add("[nome completo]", "FullName");
            PlaceHolders.Add("[nomecompleto]", "FullName");
            PlaceHolders.Add("[città]", "CityName");
            PlaceHolders.Add("[citta]", "CityName");
            PlaceHolders.Add("[totale]", "PaymentAmount");
            PlaceHolders.Add("[lampade]", "Lamps");
            PlaceHolders.Add("[c.a.p.]", ">CitiesCustomers|ZIP");
            PlaceHolders.Add("[cap]", ">CitiesCustomers|ZIP");
            PlaceHolders.Add("[provincia]", ">CitiesCustomers|District");
            PlaceHolders.Add("[prov]", ">CitiesCustomers|District");
            PlaceHolders.Add("[indirizzo]", "MainAddress");
            PlaceHolders.Add("[stato]", ">CitiesCustomers|State");
            PlaceHolders.Add("[data]", "!d");
        }

        protected override void OnDataChanged(EventArgs e)
        {
            base.OnDataChanged(e);
            customersBindingSource.DataSource = Data;
            citiesBindingSource.DataSource = Data;
        }

        private void dgCustomers_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            OnRecordCountChanged(new RecordCountChangedEventArgs(Title, dgCustomers.Rows.Count));
        }

        private void dgCustomers_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            OnRecordCountChanged(new RecordCountChangedEventArgs(Title, dgCustomers.Rows.Count));
        }

        private void dgCustomers_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            OnRecordCountChanged(new RecordCountChangedEventArgs(Title, dgCustomers.Rows.Count));
        }

        protected override void OnFilterApply(EventArgs e)
        {
            base.OnFilterApply(e);
            customersBindingSource.Filter = GetFilters();
        }

        protected override void OnFilterReset(EventArgs e)
        {
            base.OnFilterReset(e);

            customersBindingSource.RemoveFilter();
        }

        private void CustomersDatagrid_Load(object sender, EventArgs e)
        {
            if ((ParentForm != null) && (ParentForm.MainMenuStrip != null))
                System.Windows.Forms.ToolStripManager.Merge(mnuPrint, ParentForm.MainMenuStrip);
        }

        private void miMessagesMailing_Click(object sender, EventArgs e)
        {
            miMessagesMailing.Enabled = false;
            PerformMailing();
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            WorkInWord(e.Argument as WorkerArgument);
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            miMessagesMailing.Enabled = true;
            MessageBox.Show("L'elaborazione delle comunicazioni in Microsoft Word è terminata.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void miMessagesBills_Click(object sender, EventArgs e)
        {
            miMessagesBills.Enabled = false;
            PerformBillPrinting();
        }

        private void workerBills_DoWork(object sender, DoWorkEventArgs e)
        {
            PrintBills(e.Argument as BillWorkArguments);
        }

        private void workerBills_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            miMessagesBills.Enabled = true;
            MessageBox.Show("La stampa dei bollettini è terminata.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
